// ignore_for_file: prefer_const_constructors

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutterdemo/components/img.dart';
import 'package:flutterdemo/components/shop_cart.dart';
import '../../common/calc.dart';

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  // 轮播图
  Widget _swiperW() {
    return Container(
        child: AspectRatio(
      aspectRatio: 16 / 9,
      child: Swiper(
        onTap: (e){
          
        },
        // layout: SwiperLayout.STACK,
        // autoplayDisableOnInteraction:false,
        // control:null,
        // itemWidth: 320.0,
        // itemHeight: 200.0,
        autoplay: true,
        itemBuilder: (BuildContext context, int index) {
          return Img(
              "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwebsite201710.oss-cn-shanghai.aliyuncs.com%2Fjnw9%2Fupload%2Fimages%2Fimages12%2F1485610604-1498540402128758.jpg&refer=http%3A%2F%2Fwebsite201710.oss-cn-shanghai.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642233784&t=f1ee1e5d18b88dde0764a28c9d9a3440");
        },
        itemCount: 6,
        pagination: SwiperPagination(),
      ),
    ));
  }

  // 标题
  Widget _titleW(String title) {
    return Container(
      padding: EdgeInsets.only(left: Calc.width(12)),
      decoration: BoxDecoration(
          border: Border(
              left:
                  BorderSide(color: Theme.of(context).accentColor, width: 3))),
      child: Text(
        title,
        style: TextStyle(fontSize: Calc.width(32)),
      ),
    );
  }

  // 猜你喜欢每一项
  Widget _likeItem() {
    return Container(
      child: Column(
        children: [
          Container(
              width: Calc.width(150),
              height: Calc.width(150),
              child: Img(
                  "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic15.nipic.com%2F20110724%2F1773545_140759587360_2.jpg&refer=http%3A%2F%2Fpic15.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642233784&t=00412eaefbff708093f3705372402abd")),
          Text("精品衣柜")
        ],
      ),
    );
  }

  // 猜你喜欢
  Widget _likeW() {
    List<Widget> arr = [];
    for (var i = 0; i < 20; i++) {
      arr.add(_likeItem());
      if (i != 19) {
        arr.add(SizedBox(width: Calc.width(20)));
      }
    }
    return Container(
      height: Calc.width(190),
      child: ListView(
        physics: BouncingScrollPhysics(),
        scrollDirection: Axis.horizontal,
        children: arr,
      ),
    );
  }

  // 主程序
  @override
  Widget build(BuildContext context) {
    Calc.init(context);
    return ListView(
      physics: BouncingScrollPhysics(),
      children: [
        _swiperW(),
        SizedBox(
          height: 20,
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(Calc.width(30), 0, Calc.width(30), 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _titleW("猜你喜欢"),
              SizedBox(height: Calc.width(20)),
              _likeW(),
              SizedBox(height: Calc.width(40)),
              _titleW("热门推荐"),
              SizedBox(height: Calc.width(20)),
              ShopCart()
            ],
          ),
        ),
      ],
    );
  }
}
